const config = require("../db/config");
const log4js = require('../utils/logUtils');
const logger = log4js.getLogger('service');
const directoryUtils = require("../utils/directoryUtils");
const schedule = require("node-schedule");

const TAG = "[计划任务]";
let mySchedule;
module.exports = {
    // 每 config.record.duration 分钟检查一次
    async start() {
        // 检查是否需要清理空间
        const isExceed = directoryUtils.checkRecordSpace(config.record.path, config.record.maxSpace);
        if(isExceed){
            logger.info(TAG, "[检查录像] 超过限制，开始清理", config.record.path);
            directoryUtils.clearRecordSpace(config.record.path );
        }else{
            logger.info(TAG, "[检查录像] 未超限制，不需要清理", config.record.path);
        }
    },
    async startSchedule() {
        const that = this;
        await that.start();
        mySchedule= schedule.scheduleJob(`*/1 * * * *`, async () => {
            await that.start();
        });
    },
    stop() {
        if(mySchedule){
            mySchedule.cancel();
        }
    }
};
